package swing.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.TreeCellRenderer;


@SuppressWarnings("serial")
public class SelectableTreeNodeRenderer extends JPanel implements
		TreeCellRenderer {
	protected TristateCheckBox check;

	protected TreeLabel label;

	public SelectableTreeNodeRenderer() {
		this(false);
	}

	public SelectableTreeNodeRenderer(boolean showOrdering) {
		setLayout(null);
		add(check = new TristateCheckBox());
		add(label = new TreeLabel());
		check.setEnabled(!showOrdering);
		check.setBackground(UIManager.getColor("Tree.textBackground")); //$NON-NLS-1$
		label.setForeground(UIManager.getColor("Tree.textForeground")); //$NON-NLS-1$
	}

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean isSelected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		String stringValue = tree.convertValueToText(value, isSelected,
				expanded, leaf, row, hasFocus);
		setEnabled(tree.isEnabled());

		Icon icon = null;
		if (value instanceof SelectableTreeNode) {
			check.setState(((SelectableTreeNode) value).getState());
			icon = ((SelectableTreeNode) value).getIcon(expanded);
		}

		label.setFont(tree.getFont());
		label.setText(stringValue);
		label.setSelected(isSelected);
		label.setFocus(hasFocus);

		if (icon != null) {
			label.setIcon(icon);
		} else {
			if (leaf) {
				label.setIcon(UIManager.getIcon("Tree.leafIcon")); //$NON-NLS-1$
			} else if (expanded) {
				label.setIcon(UIManager.getIcon("Tree.openIcon")); //$NON-NLS-1$
			} else {
				label.setIcon(UIManager.getIcon("Tree.closedIcon")); //$NON-NLS-1$
			}
		}
		return this;
	}

	public Dimension getPreferredSize() {
		Dimension d_check = check.getPreferredSize();
		Dimension d_label = label.getPreferredSize();
		return new Dimension(d_check.width + d_label.width + 2,
				(d_check.height < d_label.height ? d_label.height
						: d_check.height));
	}

	public void doLayout() {
		Dimension d_check = check.getPreferredSize();
		Dimension d_label = label.getPreferredSize();
		int y_check = 0;
		int y_label = 0;
		if (d_check.height < d_label.height) {
			y_check = (d_label.height - d_check.height) / 2;
		} else {
			y_label = (d_check.height - d_label.height) / 2;
		}

		check.setBounds(0, y_check, d_check.width, d_check.height);
		label.setBounds(d_check.width + 2, y_label, d_label.width,
				d_label.height);
	}

	public void setBackground(Color color) {
		if (color instanceof ColorUIResource)
			color = null;
		super.setBackground(color);
	}

	private class TreeLabel extends JLabel {
		boolean isSelected;

		boolean hasFocus;

		public void setBackground(Color color) {
			if (color instanceof ColorUIResource)
				color = null;
			super.setBackground(color);
		}

		public void paint(Graphics g) {
			String str = getText();
			if (str != null) {
				if (0 < str.length()) {
					if (isSelected)
						g.setColor(UIManager
								.getColor("Tree.selectionBackground")); //$NON-NLS-1$
					else
						g.setColor(UIManager.getColor("Tree.textBackground")); //$NON-NLS-1$

					Dimension d = getPreferredSize();
					int imageOffset = 0;
					Icon currentI = getIcon();
					if (currentI != null) {
						imageOffset = currentI.getIconWidth()
								+ Math.max(0, getIconTextGap() - 1);
					}
					g.fillRect(imageOffset, 0, d.width - 1 - imageOffset,
							d.height);
					if (hasFocus) {
						g.setColor(UIManager
								.getColor("Tree.selectionBorderColor")); //$NON-NLS-1$
						g.drawRect(imageOffset, 0, d.width - 1 - imageOffset,
								d.height - 1);
					}
				}
			}
			super.paint(g);
		}

		public Dimension getPreferredSize() {
			Dimension retDimension = super.getPreferredSize();
			if (retDimension != null) {
				retDimension = new Dimension(retDimension.width + 3,
						retDimension.height + 5);
			}
			return retDimension;
		}

		public void setSelected(boolean isSelected) {
			this.isSelected = isSelected;
		}

		public void setFocus(boolean hasFocus) {
			this.hasFocus = hasFocus;
		}
	}
}
